// pages/mine/repair/repairSubmit/repairSubmit.js
const app = getApp()
import util from "../../../../utils/util.js"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type:null,//报修分类 new重新提交  edit编辑  submit提交报修
    editId:null,//报修ID
    roomArr:[],//房源数据
    house_id:null,//房源ID
    house_type:null,//房源类型
    room_number: "",//房号
    tel: "",//联系电话
    title: "",//报修说明
    image: [],//添加图片
    imageLength:0,//图片个数
    note: "",//备注
    noteLength:0,
    showInput: false,

    roomShow: false,
    roomPicker: [],//房源房号
    roomCurrentIndex: 0,
    roomLoading: false,

    fix:true,
    backNum:1,//返回页面数
    pageTotal:[],
    repairpage:null,
    pageIndex:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getRooms()
    this.setData({
      editId: options.id || null,
      type: options.type || null,
    })
    if(options.type == 'submit'){
      wx.setNavigationBarTitle({
        title: '提交报修',
      })
    }else if(options.type == 'edit'){
      wx.setNavigationBarTitle({
        title: '编辑',
      })
      this.showEditInfo()
    }else if(options.type == 'new'){
      wx.setNavigationBarTitle({
        title: '重新提交',
      })
      this.showEditInfo()
    }
  },
  onShow:function(){
    let pageTotal = getCurrentPages();
    let thispage = pageTotal[pageTotal.length - 1]//当前页面
    let parpage = pageTotal[pageTotal.length - 2]//上一个页面
    //获取repair页面index
    let index = pageTotal.findIndex((item,index) => {
        return item.route == "pages/mine/repair/repair"
    })
    let repairpage = pageTotal[index]//repair页面
    if(pageTotal.length == 3){//length为3说明是从repair直接点进来的
      this.setData({ 
        backNum: 1,//返回页面数
        repairpage:repairpage,
        pageIndex:index
      })
    }else if(pageTotal.length == 4){//length为4说明是从repair到detail再到submit页面
      this.setData({ 
        backNum: 2,//返回页面数
        repairpage:repairpage,
        pageIndex:index
      })
    }
    // console.log(index)
    // console.log(pageTotal)
  },
  /**
   * 数据更改
   */
  onChange(e) {
    this.setData({
      [e.currentTarget.dataset.prop]: e.detail
    })
  },
  /**
   * textarea点击
   */
  changeTap(e){
    this.setData({
      showInput: e.currentTarget.dataset.type
    })
  },
  /**
   * textarea输入
   */
  changeInput(e) {
    this.setData({
      note: e.detail.value,
      noteLength: e.detail.value.length
    })
    if(this.data.noteLength > 100){
      wx.showToast({
        title: '备注限制输入100个字符',
        icon: "none"
      })
    }
  },
  /**
   * textarea失去焦点
   */
  changeBlur(e){
    this.setData({
      showInput: e.currentTarget.dataset.type
    })
  },
  // 房源picker
  showRoomPicker() {
    this.setData({ roomShow: true })
    this.selectComponent("#roomPickerId").setValues([this.data.room_number])
  },
  roomCancel() {
    this.setData({ roomShow: false })
    this.selectComponent("#roomPickerId").setIndexes([this.data.roomCurrentIndex])
  },
  roomConfirm(event) {
    console.log(event)
    let arr = event.detail.value.keyId.split("_")
    console.log(arr)
    this.setData({
      roomShow: false,
      roomCurrentIndex: event.detail.index,
      room_number: event.detail.value.text,
      house_id: arr[0],
      house_type: arr[1],
    })
    this.selectComponent("#roomPickerId").setIndexes([this.data.roomCurrentIndex])
  },
  /**
   * 上传图片和删除 同一个方法
   */
  uploadImage(res){
    this.data.image = res.detail.fileList.map((item,index) => {
      return item.url
    })
    this.setData({
      imageLength: res.detail.fileList.length
    })
  },
  // 获取--点击报修显示房源
  getRooms() {
    this.setData({ roomLoading: true })
    app.globalData.request("get", "applet/repair/repairAdd", {
      id: wx.getStorageSync("id")
    })
      .then(res => {
        let arrs = res.data.housing
        arrs = arrs.map(item => {
          return {
            keyId: item.h_id + '_' + item.h_type,
            text: item.c_number
          }
        })
        this.setData({
          roomPicker: arrs,
          roomArr: arrs,
          tel: res.data.tel,
          roomLoading: false,
        })
      }).catch(err => {
        this.setData({ roomLoading: false })
        wx.showToast({
          title: err.msg,
        })
      })
  },
  // 提交确定
  submitItem(){
    if (!this.data.room_number) {
      wx.showToast({
        title: '请选择房号',
        icon: "none"
      })
    } else if (!this.data.title) {
      wx.showToast({
        title: '请输入报修说明',
        icon: "none"
      })
    } else if (!(/^1[3456789]\d{9}$/.test(this.data.tel))) {
      wx.showToast({
        title: '请正确输入联系电话',
        icon: "none"
      })
    } else if(this.data.noteLength > 100){
      wx.showToast({
        title: '备注限制输入100个字符',
        icon: "none"
      })
    }else {
      if (this.data.type == "edit" || this.data.type == "new") {
        this.repairsData()
      } else if (this.data.type == "submit") {
        app.globalData.request('post', "applet/repair/repairAdd", {
          user_id: wx.getStorageSync("id"),
          house_id: this.data.house_id,
          type: this.data.house_type,
          room_number: this.data.room_number,
          title: this.data.title,
          image: this.data.image,
          tel: this.data.tel,
          remark: this.data.note,
        })
          .then(res => {
            wx.showToast({
              title: res.msg,
            })
            this.data.repairpage.setData({
              parameter: 'addSuccess'
            })
            setTimeout(_ => {
              wx.navigateBack({
                delta: this.data.backNum
              })
            }, 1000)
          }).catch(err => {
            wx.showToast({
              title: err.mag,
              icon: "none"
            })
          })
      }
    }
  },
  // 报修编辑显示
  showEditInfo(){
    app.globalData.request("get","applet/repair/repairEdit",{
      repair_id: this.data.editId,
      id:wx.getStorageSync("id")
    })
    .then(res => {
      // let index = this.getIndex(res.data.room_number)
      this.setData({
        room_number: res.data.room_number,
        tel: res.data.tenant_tel,
        title: res.data.title,
        note: res.data.remark,
        image: res.data.image,
        imageLength: res.data.image.length,
        noteLength: res.data.remark.length,
      })
    }).catch(err => {
      wx.showToast({
        title: err.msg,
        icon:"none"
      })
    })
  },
  // picker--获取索引
  // getIndex(item){
  //   let arr = this.data.roomArr
  //   let index = arr.findIndex(i => {
  //     return item == i.text
  //   })
  //   return index
  // },
  /**
   * 编辑和重新提交--点击确认
   */
  repairsData(){
    let url = ""
    if(this.data.type == "edit"){
      url = "applet/repair/repairEdit"
    }else if(this.data.type == "new"){
      url = "applet/repair/submission"
    }
    app.globalData.request("post",url,{
      repair_id:this.data.editId,
      title:this.data.title,
      tel:this.data.tel,
      remark:this.data.note,
      image:this.data.image,
      id:wx.getStorageSync("id")
    })
    .then(res => {
      wx.showToast({
        title: res.msg,
      })
      this.data.repairpage.setData({
        parameter:"addSuccess"
      })
      setTimeout(_ => {
        wx.navigateBack({
          delta:this.data.backNum
        })
      },1000)
    }).catch(err => {
      wx.showToast({
        title: err.msg,
        icon:"none"
      })
    })
  }
})